package com.fj.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fj.config.SendSmsConfig;
import com.fj.service.SendSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendSmsImpl implements SendSmsService {

    @Autowired
    private SendSmsConfig sendSmsConfig;

    @Override
    public void sendSms(String phoneNumber,String verifications) {
        DefaultProfile profile = DefaultProfile.getProfile(sendSmsConfig.getRegionId(), sendSmsConfig.getAccessKeyId(), sendSmsConfig.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sendSmsConfig.getDomain());
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", sendSmsConfig.getRegionId());
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", sendSmsConfig.getSignName());
        request.putQueryParameter("TemplateCode", sendSmsConfig.getTemplateCode());
        request.putQueryParameter("TemplateParam", "{code:"+verifications+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
